import { useUserStore } from '@/stores';
import request from '@/utils/request'

const userStore = useUserStore();

export const videoGetService = () => {
  if (userStore.token) {
    return request.get('/video/api/me/list');
  }
  return request.get('/video/api/public/list');
}

export const videoGetByIdService = (videoId) => {
  return request.get(`/video/api/public?videoId=${videoId}`);
}

export const videoPostService = (formModel, formFileModel) => {
  const formData = new FormData();

  for (const key in formModel) {
    if (formModel.hasOwnProperty(key)) {
      formData.append(key, formModel[key]);
    }
  }
  for (const key in formFileModel) {
    if (formFileModel.hasOwnProperty(key)) {
      formData.append(key, formFileModel[key]);
    }
  }


  return request.post('/video/api/me', formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  });
}

export const videoPostStartService = (fileSize, totalChunks) => {
    return request.post(`/video/api/me/start?fileSize=${fileSize}&totalChunks=${totalChunks}`)
}

export const videoPostPartService = (uploadId, partNumber, fileBlob) => {
  const formData = new FormData();
  formData.append('uploadId', uploadId);
  formData.append('partNumber', partNumber);
  formData.append('file', fileBlob, uploadId + "-" + partNumber);

    return request.post('/video/api/me/part', formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  });
}

export const videoPostEndService = (uploadId) => {
  return request.post(`/video/api/me/end?uploadId=${uploadId}`);
}

export const videoLikeService = (videoId) => {
  return request.post(`/video/api/me/interaction/like/${videoId}`);
}

export const videoUnLikeService = (videoId) => {
  return request.delete(`/video/api/me/interaction/like/${videoId}`);
}

export const videoFavoriteService = (videoId) => {
  return request.post(`/video/api/me/interaction/favorite/${videoId}`);
}

export const videoUnFavoriteService = (videoId) => {
  return request.delete(`/video/api/me/interaction/favorite/${videoId}`);
}

export const videoCommentService = (videoId, {parentId, content}) => {
  return request.post(`/video/api/me/interaction/comment/${videoId}`, {parentId, content});
}

export const videoGetDirectCommentService = (videoId, lastMinId) => {
  return request.get(`/video/api/me/interaction/comment/${videoId}${lastMinId ? `?lastMinId=${lastMinId}` : ''}`);
}

export const videoGetReplyCommentService = (commentId, lastMaxId) => {
  return request.get(`/video/api/me/interaction/comment/reply/${commentId}${lastMaxId ? `?lastMaxId=${lastMaxId}` : ''}`);
}

export const videoGetPublishedVideo = (lastMinId) => {
  return request.get(`/video/api/me/published${lastMinId ? `?lastMinId=${lastMinId}` : ''}`);
}

export const videoGetLikeVideo = (lastMinId) => {
  return request.get(`/video/api/me/like${lastMinId ? `?lastMinId=${lastMinId}` : ''}`);
}

export const videoGetFavoriteVideo = (lastMinId) => {
  return request.get(`/video/api/me/favorite${lastMinId ? `?lastMinId=${lastMinId}` : ''}`)
}